package com.bestv.bsr.standlone.service.impl.ah;

import cn.hutool.core.collection.CollUtil;
import com.bestv.bsr.core.annotations.OptionalComponent;
import com.bestv.bsr.core.constants.AiSourceConstants;
import com.bestv.bsr.core.constants.RecommendMethod;
import com.bestv.bsr.core.constants.ServiceCodeEnum;
import com.bestv.bsr.core.context.RecommendContext;
import com.bestv.bsr.core.entities.vo.RecDataVo;
import com.bestv.bsr.core.exceptions.ServiceException;
import com.bestv.bsr.core.service.impl.ah.AbstractAHRecommendEngine;
import com.bestv.bsr.core.service.impl.ah.dto.AHRequestParam;
import com.bestv.bsr.core.service.impl.ah.dto.AHResponseDto;
import com.bestv.bsr.core.service.impl.ah.dto.AHZYWLResponseDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * todo convert sceneId
 * local ah recommend engine
 *
 * @author taojiacheng
 */
@Slf4j
@RequiredArgsConstructor
@OptionalComponent(value = "ahRecommendEngine", strategy = OptionalComponent.Strategy.STANDALONE)
public class LocalAHRecommendEngine extends AbstractAHRecommendEngine {


    @Override
    public RecDataVo recommend(RecommendContext recommendContext) {
        String methodType = recommendContext.getMethodType();
        RecommendMethod recommendMethod = RecommendMethod.fromCode(methodType);
        switch (recommendMethod) {
            case HOT_PLAY:
                return requestHotPlay(recommendContext);
            case ITEM_BASE:
                return requestRelative(recommendContext);
            case COLLABORATIVE_FILTERING:
                return requestGuess(recommendContext);
            default:
                throw new ServiceException(ServiceCodeEnum.METHODTYPE_NOT_SUPPORTED);
        }
    }

    /**
     * 请求“相关推荐”接口
     */
    protected RecDataVo requestRelative(RecommendContext recommendContext) {
        recommendContext.setCateName("98");
        AHRequestParam ahRequestParam = buildRequestParam(recommendContext, true);
        AHZYWLResponseDto ahzywlResponseDto = ahRemoteService.recommend(ahRequestParam);
        AHResponseDto ahResponseDto = handlerZYWLResp(ahzywlResponseDto);
        return convertResult(ahResponseDto, recommendContext);
    }


    /**
     * 请求“猜你喜欢”接口
     */
    public RecDataVo requestGuess(RecommendContext recommendContext) {
        // 首页推荐填98, 如果瀑布流上的分类和移动接口对不上填99
        String cateName = "99";
        Map<String, String> itemTypeMap = properties.getItemTypeMap();
        String type = itemTypeMap.get(recommendContext.getItemTypes().get(0));
        if (StringUtils.isNotEmpty(type)) {
            cateName = type.substring(0, type.indexOf(":"));
        }
        recommendContext.setCateName(cateName);
        AHRequestParam ahRequestParam = buildRequestParam(recommendContext, false);
        AHZYWLResponseDto responseDto = ahRemoteService.recommend(ahRequestParam);
        AHResponseDto ahResponseDto = handlerZYWLResp(responseDto);
        return convertResult(ahResponseDto, recommendContext);
    }


    /**
     * 请求“热门推荐”接口
     */
    public RecDataVo requestHotPlay(RecommendContext recommendContext) {
        recommendContext.setCateName("98");
        AHRequestParam ahRequestParam = buildRequestParam(recommendContext, false);
        AHZYWLResponseDto responseDto = ahRemoteService.recommend(ahRequestParam);
        AHResponseDto ahResponseDto = handlerZYWLResp(responseDto);
        return convertResult(ahResponseDto, recommendContext);
    }




    @Override
    public void validate(RecommendContext recommendContext) {
        // 参数校验
        String mac = recommendContext.getMac();
        if (StringUtils.isBlank(mac)) {
            throw new ServiceException(ServiceCodeEnum.MAC_IS_BLANK);
        }
        String userAccount = recommendContext.getUserAccount();
        if (StringUtils.isBlank(userAccount)) {
            throw new ServiceException(ServiceCodeEnum.USER_ACCOUNT_IS_BLANK);
        }
        List<String> itemTypes = recommendContext.getItemTypes();
        Map<String, String> itemTypeMap = properties.getItemTypeMap();
        List<String> intersection = itemTypes.stream().filter(itemTypeMap::containsKey).collect(Collectors.toList());
        if (CollUtil.isEmpty(intersection)) {
            throw new ServiceException(ServiceCodeEnum.ITEM_TYPES_NOT_SUPPORTED);
        }
    }

    @Override
    protected List<String> addSupportedAiSource() {
        return Collections.singletonList(AiSourceConstants.AI_SOURCE_ANYD_ZN);
    }
}
